<?php

namespace Ihanpy\Core\Base;

class Controller
{
    private $viewData = [];

    public function assign($name, $value)
    {
        $this->viewData[$name] = $value;
    }

    public function display($name)
    {
        $className = get_called_class();
        $lastPos = strrpos($className, '\\');
        $controller = substr($className, $lastPos + 1);
        $view = str_replace('Controller', '', $controller);
        $viewFile = VIEW . $view . DIRECTORY_SEPARATOR . $name . '.php';
        if (!is_file($viewFile)) {
            // todo 抛异常
        }
        extract($this->viewData);
        require $viewFile;
    }
}